﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SoftwareConsulting.BI11.Studio.Core.Project;
using SoftwareConsulting.BI11.Studio.Core.Project.SituationsHierarchy;
using SoftwareConsulting.BI11.Studio.Core.Project.Metadata;
using SoftwareConsulting.BI11.Studio.UI.ProjectExplorer.Extensions;
using SoftwareConsulting.BI11.Studio.InternalServices;
using SoftwareConsulting.BI11.Controls.Metadata;

namespace SoftwareConsulting.BI11.Studio.UI.ProjectExplorer.TimeSeries
{
    /// <summary>
    /// Диалог выбора активного расчета для временного ряда
    /// </summary>
    public partial class SelectActiveCalculationDialog : RestorableForm
    {
        private ICalculation _ActiveCalculation;
        private ProjectDescriptor _projectLink;
        private ITimeSeries _curSeries;

        public ICalculation ActiveCalculation
        {
            get
            {
                return _ActiveCalculation;
            }
        }

        /// <summary>
        /// Инициализация диалога
        /// </summary>
        private void Initialize()
        {
            this.SetObjectViewerCaption(this.Text, _curSeries);
            this.Icon = MetadataObjectKind.TimeSeries.GetIcon();
            _ActiveCalculation = null;
            var calcs = _curSeries.OwningCalculations;
            var query = from calc in calcs
                        orderby calc.Label
                        select calc.Label;
            CalculationsBox.Items.AddRange(query.ToArray());            
            if (CalculationsBox.Items.Count > 0)
            {
                CalculationsBox.SelectedIndex = 0;
            }
            else
            {
                OKButton.Enabled = false;
            }
        }

        public SelectActiveCalculationDialog(ProjectDescriptor currentProject, ITimeSeries timeSeries)
        {
            _projectLink = currentProject;
            _curSeries = timeSeries;
            InitializeComponent();
        }

        private void SelectActiveCalculationDialog_Load(object sender, EventArgs e)
        {
            Initialize();
        }

        private void CalculationsBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            _ActiveCalculation = _projectLink.RepositoryLink.GetObjectDescriptorByLabel(CalculationsBox.SelectedItem as string, MetadataObjectKind.Calculation).Bind() as ICalculation;
        }

        private void CalculationsBox_DoubleClick(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
            Close();
        }
    }
}
